package cn.psvmc.cxzapi.controller.store;

import cn.psvmc.cxzapi.model.common.DetailEntity;
import cn.psvmc.cxzapi.model.common.SearchEntity;
import cn.psvmc.cxzapi.model.store.TStore;
import cn.psvmc.cxzapi.model.store.TStoreDetail;
import cn.psvmc.cxzapi.response.*;
import cn.psvmc.cxzapi.service.store.TStoreService;
import cn.psvmc.cxzapi.util.PageUtil;
import cn.psvmc.cxzapi.util.ToolUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 商家管理
 * @Author auto
 * @Date 2024-08-14
 */
@RestController
@RequestMapping("/store")
public class TStoreController {

    @Resource
    private TStoreService tStoreService;

    /**
     * @Description 添加时保存
     * @Date 2024/8/14 21:48
     * @Author YWX
     * @Param [tStore]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/add")
    public Result add(@RequestBody TStore tStore) {
        ToolUtils.validation(new Object[]{
                tStore.getStorename(), "店铺名称", 2
                , tStore.getStoreaddress(), "商家地址", 2
                , tStore.getStorelink(), "商家联系人", 2
                , tStore.getStorephone(), "商家电话", 2
        });
        tStore.setStoretype(0);
        tStoreService.add(tStore);
        return ResultGenerator.genSuccessResult("保存成功！");
    }

    /**
     * @Description 提交
     * @Date 2024/8/14 21:48
     * @Author YWX
     * @Param [tStore]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/commit")
    public Result commit(@RequestBody TStore tStore) {
        ToolUtils.validation(new Object[]{
                tStore.getStorename(), "店铺名称", 2
                , tStore.getStoreaddress(), "商家地址", 2
                , tStore.getStorelink(), "商家联系人", 2
                , tStore.getStorephone(), "商家电话", 2
        });
        tStore.setStoretype(1);
        if (ToolUtils.isTrueInteger(tStore.getStoreid())) {//编辑时提交
            ToolUtils.validation(new Object[]{tStore.getRversion(), "版本", 1});
            tStoreService.edit(tStore);
        } else {//添加时提交
            tStoreService.add(tStore);
        }
        return ResultGenerator.genSuccessResult("提交成功！");
    }

    /**
    * @Author auto 2024-08-14 更新商家表
    * @Param tStore 商家表模型
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/edit")
    public Result edit(@RequestBody TStore tStore) {
        ToolUtils.validation(new Object[]{tStore.getStoreid(), "id", 1, tStore.getRversion(), "版本", 1});
        TStoreDetail detail = tStore.getDetail();
        if (detail != null && !ToolUtils.isTrueInteger(detail.getSdid())) {
            return ResultGenerator.genFailResult("商家详情不能为空！");
        }
        tStore.setStoretype(0);
        tStoreService.edit(tStore);
        return ResultGenerator.genSuccessResult("更新成功！");
    }

    /**
    * @Author auto 2024-08-14 查询商家表详情
    * @Param entity
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/detail")
    public Result detail(@RequestBody DetailEntity entity) {
        Integer id = entity.getId();
        ToolUtils.validation(new Object[]{id, "id", 1});
        return ResultGenerator.genSuccessResult("查询成功", tStoreService.detail(id));
    }

    /**
     * @Description 平台管理列表
     * @Date 2024/8/14 21:03
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/list")
    public PageResult2 list(@RequestBody SearchEntity entity) {
        Page<TStore> queryPage = new Page<>(entity.getPage(), entity.getSize());
        return PageUtil.getPageResult2(tStoreService.list(queryPage, entity));
    }

    /**
     * @Description 商家选择列表
     * @Date 2024/8/21 15:57
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/list_sel")
    public Result listForSel(@RequestBody SearchEntity entity) {
        return ResultGenerator.genSuccessResult("获取成功！", tStoreService.listForSel(entity));
    }

    /**
     * @Description 审核
     * @Date 2024/8/14 21:09
     * @Author YWX
     * @Param [tStore]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/check")
    public Result check(@RequestBody TStore tStore) {
        ToolUtils.validation(new Object[]{tStore.getStoreid(), "id", 1, tStore.getRversion(), "版本", 1, tStore.getCheckStatus(), "审核状态", 1});
        tStoreService.check(tStore);
        return ResultGenerator.genSuccessResult("更新成功！");
    }

    /**
     * @Description 获取商家服务信息
     * @Date 2024/9/27 12:55
     * @Author YWX
     * @Param []
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/get_service")
    public Result getService() {
        return ResultGenerator.genSuccessResult("查询成功！", tStoreService.getService());
    }

    /**
     * @Description 更新商家服务信息
     * @Date 2024/9/27 12:59
     * @Author YWX
     * @Param [detail]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/edit_service")
    public Result editService(@RequestBody TStoreDetail detail) {
        ToolUtils.validation(new Object[]{
                detail.getAlipayAccount(), "企业收款支付宝账号", 2
                , detail.getPayeeName(), "收款人姓名", 2
                , detail.getServicePhone(), "客服电话", 2
                , detail.getServiceTime(), "服务时间", 2
        });
        tStoreService.editService(detail);
        return ResultGenerator.genSuccessResult("更新成功！");
    }
}
